from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import template
from AuthenticatedBase import AuthenticatedBase
from DataClass.WebsiteRec import WebsiteRec
from DataClass.SiteUser import SiteUser
from TestDataSetup import TestDataSetup
import os

class AdminBase(AuthenticatedBase):
    def AuthenticatedGet(self, user):
        force = self.request.get("force", None)
        
        breadcrumb_list = self.GetBreadcrumbList()
        breadcrumb = ''
        
        element_count = 0
        for element_list in breadcrumb_list:
            if len(element_list) >= 2:
                breadcrumb = breadcrumb + "<a href='" + element_list[1] + "'>" + element_list[0] + "</a>"
            else:
                if len(element_list) >= 1:
                    breadcrumb = breadcrumb + element_list[0]
                    
            if element_count < len(breadcrumb_list)-1:
                breadcrumb = breadcrumb + "/"
            element_count = element_count + 1
        
        CurrentWebsite = WebsiteRec.GetCurrentWebsite(self.request)
        if (not CurrentWebsite) or force:
            TestDataSetup.Setup(user)
            CurrentWebsite = WebsiteRec.GetCurrentWebsite(self.request)
                        
        CurrentUser = SiteUser.ForceAndGetSiteUserFromEmail(user.email())

        AdminAllowed = CurrentUser.AllowedForWebsite(CurrentWebsite, True)
        
        AlternateWebsites = None
        if CurrentUser:
            AlternateWebsites = CurrentUser.GetAllWebsites(CurrentWebsite, True, 50, 0)
        AllWebsites = []
        if CurrentWebsite:
            AllWebsites.append(CurrentWebsite)
        if AlternateWebsites:
            AllWebsites += AlternateWebsites
    
        template_values = {}
        template_values['breadcrumb'] = breadcrumb
        template_values['has_currentwebsite'] = CurrentWebsite != None
        template_values['currentwebsite'] = CurrentWebsite
        template_values['currentsiteuser'] = CurrentUser
        template_values['currentuser'] = user
        template_values['alternatewebsites'] = AlternateWebsites
        template_values['allwebsites'] = AllWebsites
        template_values['needs_change_site'] = len(AlternateWebsites) > 0
        template_values['adminallowed'] = AdminAllowed
        
        template_values = self.GetTemplateValuesDict(template_values, user)

        redirectpage = self.GetRedirect()
        
        if redirectpage:
            self.redirect(redirectpage)
        
        path = os.path.join(os.path.dirname(__file__), self.GetTemplateFileName())
        self.response.out.write(template.render(path, template_values))
        
    def AuthenticatedPost(self, user):
        self.request.get('key') # just doing this to prod request into existence??
        self.DoPost(user)
        
    def GetTemplateFileName(self):
        return 'adminbase.html'
        
    def GetBreadcrumbList(self):
        return [['main', 'admin']]
    
    def GetTemplateValuesDict(self, template_values, user):
        return template_values
    
    def GetRedirect(self):
        return None
    
    def DoPost(self, user):
        None